// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
   list:[],
   formType:0,
   currentPage:1,
   currentLength:10
  },
  onShow: function () {
    var that=this
    app.login().then(res=>{
      that.request()
    })
  },
  test(){
    wx.requestSubscribeMessage({
      tmplIds: ['pn3ptkGWWSBZBuR93QbtFHsJmnAio8kUQ0-JyD60bxE'],
      success (res) {
        console.log("ok")
       },fail: (err) => {console.log(err)}
    })
  },
  request(){
    var that=this
    if(that.data.currentLength===10){
    wx.request({
      url: app.globalData.prefixUrl+'/lostAndFound/'+that.data.formType+'/'+that.data.currentPage+'/10',
      header:{
        "Authorization":app.globalData.token
      },
      success(e){
        console.log(e)
        var newList=that.data.list.concat(e.data.data.records)
        that.setData({
          list:newList,
          currentPage:that.data.currentPage+1,
          currentLength:e.data.data.records.length
        })
      }
    })
  }
  },
  change1:function(e){
    var that=this
    that.setData({
      formType:0,
      currentPage:1,
      list:[],
      currentLength:10
    })
    console.log('点了')
    this.request();
    console.log('执行了')
    console.log(app.globalData.token)
  },
  change2:function(e){
    var that=this
    that.setData({
      formType:1,
      currentPage:1,
      list:[],
      currentLength:10
    })
    this.request();
  },
  change3(){
    this.setData({
      formType:3
    })
    var that=this
    wx.request({
      url: app.globalData.prefixUrl+'/seek/forms',
      header:{
        "Authorization":app.globalData.token
      },
      success(e){
        that.setData({
          list:e.data.data
        })
        console.log(e)
      }
    })
  },
  navigateTo:function(e){
    var that=this
    wx.navigateTo({
      url: '/pages/detail/home/detail?id='+e.currentTarget.dataset.id+'&formType='+that.data.formType,
    })
  },

  changePage:function(e){
    wx.navigateTo({
      url: '/pages/publish/home/publish',
    })
  },
  onReachBottom:function(e){
    this.request();
  },
  daiqu(e){
    wx.showModal({
      title: '提示',
      content: `确认要帮他代取物品吗？`,
      cancelText: '再看看',
      confirmText: '确认',
      success(){
        app.globalData.agent=true
        wx.request({
          url: app.globalData.prefixUrl+'/agent/',
          method:'post',
          header:{
            "Authorization":app.globalData.token
          },
          data:{
            userId:app.globalData.userId,
            phone:app.globalData.userInfo.phone,
            name:app.globalData.userInfo.name,
            seekId:e.currentTarget.dataset.id
          },
          success(e){
            wx.navigateTo({
              url: '../publish/agent/agent?id='+e.data.data,
            })
          }
        })
      }
    })
  }
})